package com.party.admin.web.controller.school;

import cn.jpush.api.utils.StringUtils;
import com.party.admin.web.dto.AjaxResult;
import com.party.admin.web.dto.input.common.CommonInput;
import com.party.common.paging.Page;
import com.party.core.model.school.ScTeacher;
import com.party.core.service.school.IScTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/school/teacher")
public class TeacherController {

    @Autowired
    private IScTeacherService teacherService;

    /**
     * 老师列表
     *
     * @param teacher
     * @param page
     * @param commonInput
     * @return
     */
    @RequestMapping("teacherList")
    public ModelAndView teacherList(ScTeacher teacher, Page page, CommonInput commonInput) {
        ModelAndView mv = new ModelAndView("school/teacher/teacherList");
        page.setLimit(10);
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        mv.addObject("input", commonInput);

        List<ScTeacher> teacherList = teacherService.webListPage(teacher, params, page);
        mv.addObject("teachers", teacherList);

        mv.addObject("page", page);
        mv.addObject("teacher", teacher);
        return mv;
    }

    /**
     * 去添加老师
     * @param teacherId 老师id
     * @return
     */
    @RequestMapping("teacherForm")
    public ModelAndView teacherForm(String teacherId) {
        ModelAndView mv = new ModelAndView("school/teacher/teacherForm");
        ScTeacher scTeacher = null;
        if (StringUtils.isEmpty(teacherId)) {
            scTeacher = new ScTeacher();
        } else {
            scTeacher = teacherService.get(teacherId);
        }
        mv.addObject("teacher", scTeacher);
        return mv;
    }


    @ResponseBody
    @RequestMapping("teacherSave")
    public AjaxResult teacherSave(ScTeacher teacher) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            if (StringUtils.isEmpty(teacher.getId())) {
                teacherService.insert(teacher);
            } else {
                ScTeacher teacherDb = teacherService.get(teacher.getId());
                teacherService.update(teacher);
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }
}
